#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h> 
#include <memory.h>
#include <cstring>
#include <cerrno>
#include "Comm.hpp"
#include "Mutex.hpp"
#include "Log.hpp"
using namespace LogModule;
class InetAddr
{
private:
    void Ntohs()
    {
        _port = ntohs(_local.sin_port);
    }
    void Ntoa()
    {
       // _ip = inet_ntoa(&(_local.sin_addr));
       char buffer[64];
       inet_ntop(AF_INET,&(_local.sin_addr.s_addr),buffer,sizeof(buffer));
       _ip = buffer;
    }
public:
    InetAddr()
    {

    }
    InetAddr(uint16_t port):_port(port),_ip("0.0.0.0")
    {
        _local.sin_family = AF_INET;
        _local.sin_port = htons(_port);
        _local.sin_addr.s_addr = INADDR_ANY;//任意绑定
    }
    InetAddr(const struct sockaddr_in& addr):_local(addr)
    {
        Ntohs();
        Ntoa();
    }

    ~InetAddr()
    {

    }
    const struct sockaddr* Addr()
    {
        return CONV(&_local);
    }
    int In_Size()
    {
        return sizeof(struct sockaddr_in);
    }
    std::string Ip()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }
    std::string Sock()
    {
        return _ip+std::to_string(_port);
    }
private:
    struct sockaddr_in _local;
    std::string _ip;
    uint16_t _port;
};